﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using MINERP.COM;

namespace MINERP.DAL.PhotoRindGrid
{
    public class PhotoRindGridDAL : DALBase
    {
        public List<GridSubArea> GetSubareaUseful()
        {
            //    string subAreasSql=""

            //ObjectQuery<GridSubArea> Records =
            //    DataEntites.GridSubArea.Where(" it.Type =" + (int)SubAreaType.Photo);
            //List<GridSubArea> lists = new List<GridSubArea>();
            //foreach (GridSubArea query in Records)
            //{
            //    lists.Add(query);
            //}
            //return lists;
            int type = (int) SubAreaType.Photo;
            return DataEntites.GridSubArea.Where(p => p.Type == type).ToList();
        }
        /// <summary>
        /// 根据Id取得分区信息
        /// </summary>
        /// <param name="inSubAreaId"></param>
        /// <returns></returns>
        public GridSubArea GetOneSunAreaById(int inSubAreaId)
        {
            ObjectQuery<GridSubArea> subareas = DataEntites.GridSubArea.Where("it.SubAreaID=" + inSubAreaId);
            if (subareas.Count() > 0)
            {
               
                return subareas.First();
               
            }
            else
            {
                return null;
            }
           //return  DataEntites.GridSubArea.FirstOrDefault(g => g.SubAreaID == inSubAreaId);
        }
        /// <summary>
        /// 根据分区取得所有格子信息
        /// </summary>
        /// <param name="inSubAreaId"></param>
        /// <returns></returns>
        public List<Grid> GetGridBySubAreaId(int inSubAreaId)
        {
            string gridSql = string.Format("it.GridSubArea.SubAreaID={0}", inSubAreaId);

            //List<Grid> lists = new List<Grid>();

            ////using (newSampleYPDBEntities ds=new newSampleYPDBEntities ())
            ////{
            ////    lists = ds.Grid.Where(gridSql).Include("GridSubArea").OrderBy("it.GridId").ToList();
            ////}
            //ObjectQuery<Grid> photoGrids = DataEntites.Grid.Where(gridSql).Include("GridSubArea").OrderBy("it.GridId");
            //foreach (Grid grid in photoGrids)
            //{
            //    lists.Add(grid);
            //}
            //return lists;
            return DataEntites.Grid.Where(g => g.GridSubArea.SubAreaID == inSubAreaId).ToList();
        }
        /// <summary>
        /// 根据Id取得格子信息
        /// </summary>
        /// <param name="inGridId"></param>
        /// <returns></returns>
        public Grid GetGridById(int inGridId)
        {
            Grid grid = DataEntites.Grid.FirstOrDefault(p => p.GridId == inGridId);
            return grid;
        }
        /// <summary>
        /// 根据产品Id取得照片信息
        /// </summary>
        /// <param name="inProductId"></param>
        /// <returns></returns>
        public List<photoInfo> GetPhotoByProductId(int inProductId)
        {
            //string photoSql = string.Format("it.orderInfo.OrderProductId={0}", inProductId);
            //ObjectQuery<photoInfo> photoInfos = DataEntites.photoInfo.Where(photoSql);
            //List<photoInfo> infos = new List<photoInfo>();
            //foreach (photoInfo info in photoInfos)
            //{
            //    infos.Add(info);
            //}
            //return infos;

            return DataEntites.photoInfo.Where(p => p.orderInfo.OrderProductId == inProductId).ToList();
        }
        /// <summary>
        /// 根据打印机Id取得打印机信息
        /// </summary>
        /// <param name="InPrinterId"></param>
        /// <returns></returns>
        public printer GetPrinterById(int InPrinterId)
        {
            //string printerSql = string.Format("it.PrinterID={0}", InPrinterId);
            //ObjectQuery<printer> printers = DataEntites.printer.Where(printerSql);
            //if (printers.Count() > 0)
            //{
            //    return printers.First();
            //}
            //else
            //{
            //    return null;
            //}

           return  DataEntites.printer.FirstOrDefault(p => p.PrinterID == InPrinterId);
        }
        /// <summary>
        /// 取得产品
        /// </summary>
        /// <param name="inProductId"></param>
        /// <returns></returns>
        public orderInfo GetProductById(int inProductId)
        {
            orderInfo product = DataEntites.orderInfo.FirstOrDefault(p => p.OrderProductId == inProductId);
            return product;
        }
        /// <summary>
        ///  获取产品照片
        /// </summary>
        /// <param name="inProductId"></param>
        /// <returns></returns>
        public List<photoInfo> GetPhotoOfProduct(int inProductId)
        {
            ////DataEntites.Refresh(RefreshMode.StoreWins, DataEntites.photoInfo);
            //IQueryable<photoInfo> photoInfos = DataEntites.photoInfo.Where(p => p.orderInfo.OrderProductId == inProductId);
            //List<photoInfo> lists = new List<photoInfo>();
            //foreach (photoInfo info in photoInfos)
            //{
            //    lists.Add(info);
            //}

            return DataEntites.photoInfo.Where(p => p.orderInfo.OrderProductId == inProductId).ToList();
        }
        /// <summary>
        /// 根据bacode取得照片信息
        /// </summary>
        /// <param name="inBarCode"></param>
        /// <returns></returns>
        public photoInfo GetPhotoByBarCode(string inBarCode)
        {
            int st1 = (int)PhotoStatus.Printed;
            int st2 = (int)PhotoStatus.FenPian;

            photoInfo photoshop = DataEntites.photoInfo.Where(p => p.PhotoBarCode == inBarCode && (p.status == st1 || p.status == st2)).OrderByDescending(pi => pi.photoId).FirstOrDefault();
            return photoshop;



            //photoInfo photoshop;
            //using (newSampleYPDBEntities ds=new newSampleYPDBEntities ())
            //{
            //    photoshop = ds.photoInfo.FirstOrDefault(p => p.PhotoBarCode == inBarCode && (p.status == st1 || p.status == st2));
            //}
            //return photoshop;
        }
        /// <summary>
        /// 检查反单的照片
        /// </summary>
        /// <param name="inBarCode"></param>
        /// <returns></returns>
        public photoInfo GetFanDanPhotoByBarCode(string inBarCode)
        {
            int st1 = (int)PhotoStatus.PhotoInnerReturn;
            //photoInfo photoshop;
            //using (newSampleYPDBEntities ds=new newSampleYPDBEntities ())
            //{
            //    photoshop = ds.photoInfo.FirstOrDefault(p => p.PhotoBarCode == inBarCode && (p.status == st1));
            //}

            photoInfo photoshop = DataEntites.photoInfo.FirstOrDefault(p => p.PhotoBarCode == inBarCode && (p.status == st1));
            return photoshop;
        }

        /// <summary>
        /// 根据bacode取得照片信息
        /// </summary>
        /// <param name="inBarCode"></param>
        /// <returns></returns>
        public photoInfo GetPhotoByPhototId(int inPhototId)
        {
            photoInfo photoshop = DataEntites.photoInfo.FirstOrDefault(p => p.photoId == inPhototId);
            return photoshop;
        }
        /// <summary>
        /// 根据产品获得格子信息
        /// </summary>
        /// <param name="inProductId"></param>
        /// <returns></returns>
        public Grid GetGridByProductId(int inProductId)
        {
            int status = (int)GridStatus.use;
            //Grid gird;
            //using (newSampleYPDBEntities ds=new newSampleYPDBEntities ())
            //{
            //    gird = ds.Grid.FirstOrDefault(p => p.SomeId == inProductId && p.Status == status);
            //}
            Grid gird = DataEntites.Grid.FirstOrDefault(p => p.SomeId == inProductId && p.Status == status);
            return gird;
        }
        /// <summary>
        /// 标记照片已经分配
        /// </summary>
        /// <param name="inBarCode"></param>
        public void SetPhotoComplete(string inBarCode)
        {
            List<photoInfo> photoshops = DataEntites.photoInfo.Where(p => p.PhotoBarCode == inBarCode).ToList();
            foreach (photoInfo info in photoshops)
            {
                info.status = (int)PhotoStatus.FenPian;
            }



        }
        /// <summary>
        /// 获得格子使用记录
        /// </summary>
        /// <param name="inGridId"></param>
        /// <returns></returns>
        public GridRecord GetGridRecordByGridId(int inGridId, int inPhotoId)
        {
            GridRecord gridRecord = DataEntites.GridRecord.Where(string.Format("it.Grid.GridId={0} and it.SomeId={1}", inGridId, inPhotoId)).FirstOrDefault();
            return gridRecord;
        }
        public void GetPrintInfo()
        {
        }
        /// <summary>
        /// 获得最小可用格子
        /// </summary>
        /// <param name="inSubArea"></param>
        /// <returns></returns>
        public Grid GetMinUsefulGrid(int inSubArea)
        {
            List<Grid> grids = GetGridBySubAreaId(inSubArea).OrderBy(p => Convert.ToInt32(p.GridName.Substring(1))).ToList();
            Grid rtnGrid = new Grid();
            foreach (Grid list in grids)
            {
                if (list.Status == (int)GridStatus.useAble)
                {
                    rtnGrid = list;
                    break;
                }

            }
            return rtnGrid;
        }
        /// <summary>
        /// 检查产品是否分配格子
        /// </summary>
        /// <param name="inSomeId"></param>
        /// <returns></returns>
        public bool CheckProduct(int inSomeId)
        {
            Grid grid = DataEntites.Grid.FirstOrDefault(p => p.SomeId == inSomeId);
            if (grid == null)
            {
                return false;
            }
            else
            {
                return true;
            }

        }
        /// <summary>
        /// 添加一条分片记录信息
        /// </summary>
        /// <param name="inGrid"></param>
        public void AddGridRecord(Grid inGrid)
        {
            DataEntites.AddToGrid(inGrid);
            Save();
        }
        /// <summary>
        /// 保存修改
        /// </summary>
        public void SaveChange()
        {
            Save();
        }
        /// <summary>
        /// 取得用户
        /// </summary>
        /// <param name="inUserId"></param>
        /// <returns></returns>
        public users GetUserById(int inUserId)
        {
            return DataEntites.users.FirstOrDefault(p => p.userID == inUserId);
        }
        /// <summary>
        /// 删除GridRecord记录
        /// </summary>
        /// <param name="record"></param>
        public void DeleteGridRecord(GridRecord record)
        {
            DataEntites.DeleteObject(record);
        }
        /// <summary>
        /// 根据产品获得流程
        /// </summary>
        /// <param name="inProductId"></param>
        /// <returns></returns>
        public List<WorkOrderFlowList> GetAllFlowByProductId(int inProductId)
        {
            //List<WorkOrderFlowList> lists = new List<WorkOrderFlowList>();
            //ObjectQuery<WorkOrderFlowList> flowLists =
            //    DataEntites.WorkOrderFlowList.Where(string.Format("it.orderInfo.OrderProductId={0}", inProductId));
            //foreach (WorkOrderFlowList list in flowLists)
            //{
            //    lists.Add(list);
            //}
            //return lists;

            return DataEntites.WorkOrderFlowList.Where(p => p.orderInfo.OrderProductId == inProductId).ToList();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string GetNextFlowName(int inProductId)
        {
            // int stutas = (int)OrderInfoStatus.Doing;
            // DataEntites.Refresh(RefreshMode.StoreWins, DataEntites.WorkOrderFlowList);
            //WorkOrderFlowList workflow= DataEntites.WorkOrderFlowList.Where(p => p.orderInfo.OrderProductId == inProductId&&p.Status==stutas).FirstOrDefault();

            //if (workflow == null)
            //{
            //    return null;
            //}
            //else
            //{
            //    workflow.tb_TechReference.Load();
            //    return workflow.tb_Tech.TechName;
            //}
            int stutas = (int)OrderInfoStatus.Doing;
            string techName = "";
            orderInfo oi = DataEntites.orderInfo.FirstOrDefault(o => o.OrderProductId == inProductId);
            if (oi != null)
            {
                if (oi.ProductType == (int)OrderInfoType.InnerReturn)
                {
                    return "";
                }
                //oi.WorkOrderFlowList.Load(MergeOption.OverwriteChanges);
                if (!oi.WorkOrderFlowList.IsLoaded)
                {
                    oi.WorkOrderFlowList.Load();
                }
                WorkOrderFlowList wofl = oi.WorkOrderFlowList.FirstOrDefault(wf => wf.Status == stutas);
                if (wofl != null)
                {
                    if (!wofl.tb_TechReference.IsLoaded)
                    {
                        wofl.tb_TechReference.Load();
                    }
                    techName = wofl.tb_Tech.TechName;
                }
            }
            return techName;
        }

        public void RushDateBase()
        {
            DataEntites.Refresh(RefreshMode.StoreWins, DataEntites.photoInfo);
        }
        /// <summary>
        /// 获取所有的可用商户
        /// </summary>
        /// <returns></returns>
        public List<Shop> GetShopList()
        {
           
            return DataEntites.Shop.Where(p=>p.Status==1).ToList();
        }
        public string GetShopNameByOrderId(int inOrderId)
        {
            orderInfo oi= DataEntites.orderInfo.Where(p => p.OrderProductId == inOrderId).FirstOrDefault();
            if (!oi.Tb_OrderBagReference.IsLoaded)
            {
                oi.Tb_OrderBagReference.Load();
            }
            if (!oi.Tb_OrderBag.ShopReference.IsLoaded)
            {
                oi.Tb_OrderBag.ShopReference.Load();
            }
             return oi.Tb_OrderBag.Shop.shopName;

        }

    }
}
